<?php

class RunTimeWorkflowParallelSplitWithForeachableNode extends RunTimeWorkflowParallelSplitNode
{
  /**
   * @var array
   */
  protected $run_time_nodes = array();

  protected function doDeliver ()
  {
    $this->workflow->handleWorkItemDelivered($this);
    $this->execute();
  }

  protected function doExecute ($action = null)
  {
    $out_nodes = $this->design_time_node->getOutNodes();
    $out_node = array_shift($out_nodes);
    $foreachable = $this->design_time_node->getForeachable();
    foreach ($foreachable as $item)
    {
      $run_time_node = $out_node->createRunTimeNode($this->workflow);
      $run_time_node->bind($item);
      $this->run_time_nodes[] = $run_time_node;
    }
    $this->workflow->handleWorkItemExecuted($this);
    $this->complete();
  }

  protected function doComplete ()
  {
    $this->workflow->handleWorkItemCompleted($this);
    
    foreach ($this->run_time_nodes as $run_time_node)
    {
      $run_time_node->deliver();
    }
  }
}